package com.fr.web.core.service;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.json.JSONObject;
import com.fr.base.file.DatasourceManager;
import com.fr.data.impl.Connection;
import com.fr.data.impl.JDBCDatabaseConnection;
import com.fr.web.cache.WebletFactory;
import com.fr.web.core.ShowWorkBookPolicy.Policy4Form;
import com.fr.web.core.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ConsoleService extends NoSessionIDService
{
  private static final String CONSOLE_CONNECTION = "console_connection";
  private static final String CONSOLE_REGISTER = "console_reg";
  private static final String CONSOLE_GLIC = "console_glic";
  private static final String CONSOLE_SVN = "console_svn";
  private static ConsoleService CONSOLE_SERVICE = new ConsoleService();

  public static ConsoleService getInstance()
  {
    return CONSOLE_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.startsWith("console_");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    if ("console_connection".equals(paramString))
      dealWithConnection(paramHttpServletRequest, paramHttpServletResponse);
    else if ("console_reg".equals(paramString))
      dealWithRegister(paramHttpServletRequest, paramHttpServletResponse);
    else if ("console_glic".equals(paramString))
      dealWidthGLic(paramHttpServletRequest, paramHttpServletResponse);
    else if ("console_svn".equals(paramString))
      dealWidthSVN(paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void dealWithConnection(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str3;
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "action");
    if (StringUtils.isBlank(str1))
    {
      ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/console/JDBCDatabaseConnection.cpt", new ShowWorkBookPolicy.Policy4Form()));
      return;
    }
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "form_value");
    JSONObject localJSONObject = new JSONObject(str2);
    DatasourceManager localDatasourceManager = FRContext.getDatasourceManager();
    if ("column_modify".equals(str1))
    {
      str3 = (String)localJSONObject.get("Name");
      Connection localConnection = localDatasourceManager.getConnection(str3);
      if (localConnection instanceof JDBCDatabaseConnection)
      {
        JDBCDatabaseConnection localJDBCDatabaseConnection = (JDBCDatabaseConnection)localConnection;
        localJDBCDatabaseConnection.setDriver(localJSONObject.getString("Driver"));
        localJDBCDatabaseConnection.setURL(localJSONObject.getString("URL"));
        localJDBCDatabaseConnection.setUser(localJSONObject.getString("User"));
        localJDBCDatabaseConnection.setPassword(localJSONObject.getString("Password"));
        localJDBCDatabaseConnection.setNewCharsetName((String)localJSONObject.get("newCharsetName"));
        localJDBCDatabaseConnection.setOriginalCharsetName((String)localJSONObject.get("originalCharsetName"));
      }
    }
    else if ("column_delete".equals(str1))
    {
      str3 = (String)localJSONObject.get("Name");
      localDatasourceManager.removeConnection(str3);
    }
    else if ("add".equals(str1));
  }

  private static void dealWithRegister(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/console/register.cpt", new ShowWorkBookPolicy.Policy4Form()));
  }

  private static void dealWidthGLic(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/console/generateLicense.cpt", new ShowWorkBookPolicy.Policy4Form()));
  }

  private static void dealWidthSVN(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ReportletDealWith.dealWithReportlet(paramHttpServletRequest, paramHttpServletResponse, WebletFactory.createEmbeddedReportlet("/com/fr/web/console/logInfo.cpt", new ShowWorkBookPolicy.Policy4Form()));
  }
}